
var that, page = 1, vid = '';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    background: [],
    page_count: 0,
    current: 0,
    vertical: false,
    autoplay: false,
    duration: 500,
    wdz: 'https://assets.renrentuibao.com/web%2Fuploads%2Fimage%2Fstore_1%2F20c28ab80be23cac60725b4fda7dcc2d14c5140c.png',
    dz: 'https://assets.renrentuibao.com/web%2Fuploads%2Fimage%2Fstore_1%2F2cc58616649a42883ca9ae322c43cb5dc4339d01.png',
    plwdz: 'https://assets.renrentuibao.com/assets/icon/liked.png',
    pldz: 'https://assets.renrentuibao.com/assets/icon/like.png',
    recommender_id: '',
    id: '',
    pl: false,
    comments_list: [],//评论列表
    pageindex: 1,
    page_count: 1,
    pltext: '',
    setting: {},
    touchStartTime: 0,    // 触摸开始时间
    touchEndTime: 0,    // 触摸结束时间
    lastTapTime: 0,    // 最后一次单击事件点击发生时间
    lastTapTimeoutFunc: null,    // 单击事件点击后要触发的函数
    play: true,//视频是否播放
    plvalue: '',
    remind: false,
    free: '1'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this
    page = 1
    getApp().page.onLoad(this, options),
      console.log('options', options)
    if (options.vid) {
      vid = options.vid
    }
    that.super_video_setting()
    if (wx.getStorageSync('ACCESS_TOKEN')) {
      console.log('已登录')
      if (options.recommender_id) {
        console.log('1')
        getApp().page.bindParent1(options.recommender_id)
      } else {
        console.log('2')
      }
    } else {
      console.log('未登录')
      var recommender_id = wx.getStorageSync('recommender_id')
      console.log('recommender_id==', recommender_id)
      if (options.recommender_id) {
        if (recommender_id === '' || recommender_id === '0') {
          console.log('wu')
          wx.setStorageSync('recommender_id', options.recommender_id)
        } else {
          console.log('you')
        }
      }
    }
    that.setData({
      id: options.id,
      recommender_id: options.recommender_id
    })
    if (options.free) {
      that.setData({
        remind: true,
        free: options.free
      })
    }
    that.super_video_list()
  },
  bindchange: function (e) {
    var current = e.detail.current
    var background = that.data.background
    console.log(current, background.length)
    if (page != that.data.page_count) {
      console.log(background.length, '111111', current + 1,)
      if (current + 1 == background.length) {
        console.log('22222')
        page += 1
        that.super_video_list()
      }
    }
    var videoContext = wx.createVideoContext(background[that.data.current].id)
    videoContext.stop()
    var videoContext = wx.createVideoContext(background[current].id)
    videoContext.play()
    that.setData({
      current: current,
      play: true
    })

  },
  super_video_list: function (e) {
    var background = that.data.background
    getApp().request({
      url: getApp().api.default.super_video_list,
      data: {
        page: page,
        recommend_id: that.data.recommender_id,
        id: that.data.id,
        vid: vid
      },
      success: function (t) {
        if (t.code == 0) {
          console.log('122222', t.data.list)
          for (var i = 0; i < t.data.list.length; i++) {
            background.push(t.data.list[i])
          }
          that.setData({
            background: background,
            page_count: t.data.page_count
          })
        }
      },
    });
  },
  super_video_setting: function (e) {
    var a = this
    getApp().request({
      url: getApp().api.default.super_video_setting,
      success: function (t) {
        if (t.code == 0) {
          a.setData({
            setting: t.data
          })
        }
      },
    });
  },
  goods: function (e) {
    console.log(e)
    var id = e.currentTarget.dataset.id
    if (id == '1168') {
      wx.navigateTo({
        url: '/pages/goods/goods?id=1168' + "&xr=1",
      })
    } else {
      wx.navigateTo({
        url: '/pages/goods/goods?id=' + id,
      })
    }

  },
  onShow: function (e) {
    getApp().page.onShow(this)
  },
  // /// 长按
  // longTap: function(e) {
  //   console.log("long tap")
  //   wx.showModal({
  //     title: '提示',
  //     content: '长按事件被触发',
  //     showCancel: false
  //   })
  // },
  /// 单击、双击
  multipleTap: function (e) {
    var that = this
    // 控制点击事件在350ms内触发，加这层判断是为了防止长按时会触发点击事件
    if (that.data.touchEndTime - that.data.touchStartTime < 350) {
      // 当前点击的时间
      var currentTime = e.timeStamp
      var lastTapTime = that.data.lastTapTime
      // 更新最后一次点击时间
      that.data.lastTapTime = currentTime
      // 如果两次点击时间在300毫秒内，则认为是双击事件
      if (currentTime - lastTapTime < 300) {
        console.log("double tap")
        // 成功触发双击事件时，取消单击事件的执行
        clearTimeout(that.data.lastTapTimeoutFunc);
        that.point_like()
      } else {
        // 单击事件延时300毫秒执行，这和最初的浏览器的点击300ms延时有点像。
        that.data.lastTapTimeoutFunc = setTimeout(function () {
          console.log("tap")
          that.paly()
        }, 300);
      }
    }
  },
  touchStart: function (e) {
    that.setData({
      touchStartTime: e.timeStamp
    })
  },
  touchEnd: function (e) {
    that.setData({
      touchEndTime: e.timeStamp
    })
  },
  guanb: function (e) {
    var a = this
    a.setData({
      remind: false
    })
  },
  paly: function (e) {
    var background = that.data.background
    var videoContext = wx.createVideoContext(background[that.data.current].id)
    console.log('that.data.play', that.data.play)
    if (that.data.play) {
      videoContext.pause()
      that.setData({
        play: false
      })
    } else {
      videoContext.play()
      that.setData({
        play: true
      })
    }
  },

  //评论列表
  comment: function (o) {
    var that = this;
    that.setData({
      pl: true,
      pageindex: 1,
      comments_list: []
    })
    that.video_comments_list(1)
  },
  video_comments_list: function (o) {
    var that = this;
    var background = that.data.background
    var comments_list = that.data.comments_list
    getApp().request({
      url: getApp().api.default.video_comments_list,
      data: {
        page: o,
        video_id: background[that.data.current].id
      },
      success: function (t) {
        console.log(t)
        if (t.code == 0) {
          for (var i = 0; i < t.data.list.length; i++) {
            comments_list.push(t.data.list[i])
          }
          that.setData({
            comments_list: comments_list,
            page_count: t.data.page_count
          })
          console.log(that.data)
        }
      },
    });
  },
  //评论
  oo: function (e) {
    var value = e.detail.value
    this.setData({
      plvalue: value
    })
  },
  plcomment: function (e) {
    console.log(e)
    var that = this;
    // var value = e.detail.value
    var background = that.data.background
    if (that.data.plvalue == "" || that.data.plvalue == undefined) {
      wx.showToast({
        title: '请输入评论',
        icon: 'none',
        duration: 2500
      })
    } else {
      getApp().request({
        url: getApp().api.default.add_video_comments,
        method: 'post',
        data: {
          content: that.data.plvalue,
          video_id: background[that.data.current].id
        },
        success: function (t) {
          console.log(t)
          if (t.code == 0) {
            that.setData({
              pltext: '',
              plvalue: ''
            })
            wx.showToast({
              title: '评论成功',
              icon: 'none',
              duration: 2500
            })
            that.comment()
          }
        },
      });
    }
  },
  //评论点赞
  video_comments_like: function (e) {
    console.log(e)
    var a = this
    var comments_list = a.data.comments_list
    var index = e.currentTarget.dataset.index
    getApp().request({
      url: getApp().api.default.video_comments_like,
      method: 'post',
      data: {
        comment_id: e.currentTarget.dataset.id, //评论ID
        status: 0
      },
      success: function (t) {
        if (t.code == 0) {
          comments_list[index].like_status = '1',
            comments_list[index].score = (+comments_list[index].score) + (+'1'),
            a.setData({
              comments_list: comments_list
            })
          wx.showToast({
            title: t.msg,
            icon: 'none',
            duration: 2500
          })
        } else {
          wx.showToast({
            title: t.msg,
            icon: 'none',
            duration: 2500
          })
        }
      },
      complete: function (t) {
        if (t.data.code == -1 && t.data.msg == '请先登陆') {
        }
      }
    });
  },
  //视频点赞
  point_like() {
    var that = this;
    var background = that.data.background
    getApp().request({
      url: getApp().api.default.video_like,
      // method: 'post',
      data: {
        id: that.data.id, // 商品编号
        recommend_id: that.data.recommender_id,// 推荐人id，可允许为空
        video_id: background[that.data.current].id
      },
      success: function (t) {
        if (t.code == 0) {
          background[that.data.current].is_like = true
          background[that.data.current].nums = background[that.data.current].nums + 1,
            that.setData({
              background: background
            })
        }
        wx.showToast({
          title: t.msg,
          icon: 'none',
          duration: 2500
        })
      },
      complete: function (t) {
        if (t.data.code == -1 && t.data.msg == '请先登陆') {
          wx.setStorageSync('is_like', true)
        }
      }
    });
  },
  gb: function () {
    this.setData({
      pl: false,
      pageindex: 1,
      comments_list: []
    })
  },
  daodi: function (e) {
    var a = this
    console.log(a.data.pageindex, a.data.page_count)
    if (a.data.page_count == a.data.pageindex) {
      wx.showToast({
        title: '没有更多了',
        icon: 'none',
        duration: 2500
      })
    } else {
      var pageindex = a.data.pageindex + 1
      a.setData({
        pageindex: pageindex
      })
      a.video_comments_list(pageindex)
    }
  },
  onShareAppMessage: function (e) {
    console.log(e)
    getApp().page.onShareAppMessage(this);
    var o = this, t = getApp().getUser();
    if (e.target) {
      var title = e.target.dataset.title
    } else {
      var title = '超点赞'
    }

    var background = o.data.background
    console.log('getUser', t, o, background[that.data.current].id)
    getApp().request({
      url: getApp().api.default.super_video_share,
      success: function (t) {
      },
      complete: function (t) {
      }
    });
    return {
      path: "/pages/tuibao-integral-mall/thumbsup/thumbsup?id=38" + "&recommender_id=" + t.id + "&vid=" + background[that.data.current].id,
      success: function (t) {
      },
      title: title,
      imageUrl: 'https://assets.renrentuibao.com/web%2Fuploads%2Fimage%2Fstore_1%2F6424aec60263539fb0aac52875b81ebefc7d4d88.png'
    };
  }
})